package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Course;
import com.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class CourseController {

    @Autowired
    private CourseService service;

//    查询全部和模糊查询

    @RequestMapping("/showCourseList")
    public String showCourseList(Model model, Integer page, String cName, String fukuan){
        if (page == null) {
            page = 1;
        }
        //设置分页
        PageHelper.startPage(page, 10);

        //模糊
        List<Course> courseCname = service.findCourseStyleCname(cName, fukuan);
        System.out.println("模糊查询对象："+courseCname);

//        new 分页
        PageInfo<Course> pageInfo = new PageInfo<Course>(courseCname,5);
//        保存到model
        model.addAttribute("pageInfo", pageInfo);
        if (cName == null && fukuan == null) {
            return "course";
        }
        model.addAttribute("cName", cName);
        model.addAttribute("fukuan", fukuan);
        System.out.println("分页集合" + pageInfo);
        System.out.println("需要查询的内容" + cName);
        System.out.println("上下架" + fukuan);
        return "course";
    }

//  查单个
    @RequestMapping("/findCourseById")
    public String findCourseById(Integer cId,Model model){
        Course courseById = service.findCourseStyleById(cId);
        System.out.println("查询单个集合1："+courseById);
        model.addAttribute("findById",courseById);
        return "courseView";
    }

//    修改的方法，先显示单个
    @RequestMapping("/findCourseUpdateById")
    public String findCourseUpdateById(Integer cId,Model model){
        Course courseById = service.findCourseStyleById(cId);
        System.out.println("查询单个集合1："+courseById);
        model.addAttribute("updateById",courseById);
        return "courseUpdate";
    }

//    修改
    @RequestMapping("/updateCourse")
    public String updateCourse(Course course,Model model){
        System.out.println("修改显示的对象："+course);
        boolean b = service.updateCourse(course);
        if(b=true){
            return "redirect:/showCourseList";
        }
        return "course";
    }

//  增加
    @RequestMapping("/addCourse")
    public String addCourse(Course course){
        boolean b = service.addCourse(course);
        System.out.println("增加方法的对象："+course);
        if(b=true){
            return "redirect:/showCourseList";
        }
        return "redirect:/showCourseList";
    }

//    删除
    @RequestMapping("/deleteCourse")
    public String deleteCourse(Integer cId){
        System.out.println("删除方法显示的cId："+cId);
        boolean b = service.deleteCourse(cId);
        if(b=true){
            return "course";
        }
        return "course";
    }
}
